package org.nt4b.schemadoc.core;

import java.util.List;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nt4b.schemadoc.core.dao.SchemaDocDAO;
import org.nt4b.schemadoc.core.dom.Table;
import org.nt4b.schemadoc.core.out.DocumentationOutputBuilder;
import org.nt4b.schemadoc.core.out.OutputBuilder;
import org.nt4b.schemadoc.core.out.SQLOutputBuilderFactory;
import org.nt4b.schemadoc.core.out.XMLOutputBuilder;


public class SchemaDoc {
    private static Log log = LogFactory.getLog(SchemaDoc.class);
    
    /**
     * @param args
     */
    public static void main(final String[] args) {
        final Options options = ProgrammOptions.buildOptions();
        
        if (args.length == 0) {
            SchemaDoc.printUsage(options);
        }
        
        final CommandLineParser parser = new PosixParser();
        CommandLine cmd = null;
        try {
            cmd = parser.parse(options, args, true);
        } catch (final ParseException e) {
            SchemaDoc.printUsage(options);
            throw new RuntimeException(e);
        }
        
        final String url = cmd.getOptionValue(ProgrammOptions.URL.getOpt());
        final String username = cmd.getOptionValue(ProgrammOptions.USERNAME.getOpt());
        final String password = cmd.getOptionValue(ProgrammOptions.PASSWORD.getOpt());
        final String schemaName = cmd.getOptionValue(ProgrammOptions.SCHEMA_NAME.getOpt());
        final String resultType = cmd.getOptionValue(ProgrammOptions.RESULT_TYPE.getOpt());
        
        if (log.isDebugEnabled()) {
            log.debug("Url: " + url);
            log.debug("Username: " + username);
            log.debug("Pasword: " + password);
            log.debug("ResultType: " + resultType);
        }
        
        final SchemaDocDAO schemaDocDAO = DAOFactory.getDAO(url, username, password);
        
        final List<Table> tables = schemaDocDAO.getSchemaComments(schemaName);
        
        final List<Table> filteredTables = OutputFilter.filter(tables, cmd);
        
        final OutputBuilder outputBuilder;
        if ("DOCUMENTATION".equalsIgnoreCase(resultType)) {
            outputBuilder = new DocumentationOutputBuilder();
        } else if ("UPDATE".equalsIgnoreCase(resultType)) {
            outputBuilder = SQLOutputBuilderFactory.getDBOutputBuilder(url);
        } else if ("YML".equalsIgnoreCase(resultType)) {
            outputBuilder = new XMLOutputBuilder();
        } else {
            throw new IllegalArgumentException("Unknown result type: " + resultType);
        }
        
        final String result = outputBuilder.buildResult(filteredTables);
        
        System.out.println(result);
    }
    
    private static void printUsage(final Options options) {
        final HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("java -jar schemadoc.jar", options, true);
    }
}
